import apiClient from '@/api/axios.js';

export class UserApi {

  // 创建用户
  static async createUser(params) {
    const { username, password, email } = params;
    try {
      const response = await apiClient.post('/users', {
        username,
        password,
        email
      });
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 更新用户信息
  static async updateUser(userId, params) {
    const { username, password, email } = params;
    try {
      const response = await apiClient.put(`/users/${userId}`, {
        username,
        password,
        email
      });
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 删除用户
  static async deleteUser(userId) {
    try {
      const response = await apiClient.delete(`/users/${userId}`);
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 获取单个用户
  static async getUserById(userId) {
    try {
      const response = await apiClient.get(`/users/${userId}`);
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 通过用户名获取用户
  static async getUserByUsername(username) {
    try {
      const response = await apiClient.get(`/users/username/${username}`);
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 通过邮箱获取用户
  static async getUserByEmail(email) {
    try {
      const response = await apiClient.get(`/users/email/${email}`);
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 获取所有用户
  static async getAllUsers(page,pageSize) {
    try {
      const response = await apiClient.get('/users');
      return response;
    } catch (error) {
      throw new Error(error.message);
    }
  }
}
